package com.objectville.restaurant.iterator;

import java.util.Iterator;

import com.objectville.restaurant.leaf.MenuItem;

public class DinerMenuIterator implements Iterator<MenuItem> {

	private MenuItem[] menuItemsArray;
	private int position = 0;

	public DinerMenuIterator(MenuItem[] menuItems) {
		this.menuItemsArray = menuItems;
	}

	@Override
	public boolean hasNext() {
		if (position >= menuItemsArray.length || menuItemsArray[position] == null) {
			return false;
		}
		return true;
	}

	@Override
	public MenuItem next() {
		MenuItem menuItem = menuItemsArray[position];
		position++;
		return menuItem;
	}

	@Override
	public void remove() {
		if (position <= 0) {
			throw new IllegalStateException(
					"YOu can't remove an item until you've done at least one next()");
		}
		
		if(menuItemsArray[position-1] != null ) {
			for(int i = position-1; i < (menuItemsArray.length-1); i++) {
				menuItemsArray[i] = menuItemsArray[i+1];
			}
			menuItemsArray[menuItemsArray.length-1] = null;
		}

	}

}
